package cn.holy.app.xcddt.ui.activity.record

import androidx.core.content.ContextCompat
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.repository.RecordRepository
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.android.synthetic.main.activity_state.*
import javax.inject.Inject


/**
 * Created by 69568 on 2018/2/27.
 */
@Route(path = "/app/record/state")
class StateActivity : BaseActivity() {
    @Inject
    lateinit var recordRepository: RecordRepository

    override fun initData() {

    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("选择操作").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
        //导航

        btn_continue.setOnClickListener {

            val lastRecordGroup = recordRepository.lastRecordGroup
            if (lastRecordGroup != null && lastRecordGroup.status == 0) {
                ARouter.getInstance().build("/app/record/detail").navigation()
            } else {
                showTips("暂未开始任何巡查", QMUITipDialog.Builder.ICON_TYPE_FAIL)
            }

        }
        btn_start_new.setOnClickListener {
            val lastRecordGroup = recordRepository.lastRecordGroup

            if (lastRecordGroup != null && lastRecordGroup.status == 0) {
                QMUIDialog.MessageDialogBuilder(this@StateActivity)
                        .setMessage("检测到上次巡查未结束，是否开始新的巡查")
                        .setTitle("提示")
                        .addAction("确定") { dialog, _ ->
                            lastRecordGroup.status = 1
                            recordRepository.saveRecordGroup(lastRecordGroup)
                            ARouter.getInstance().build("/app/record/start").navigation()
                            dialog.dismiss()
                        }.addAction("取消") { dialog, _ ->
                            dialog.dismiss()
                        }
                        .show()

            }else {
                ARouter.getInstance().build("/app/record/start").navigation()
            }

        }
    }

    override fun initLayout() {
        setContentView(R.layout.activity_state)
    }

}